#include <stdio.h>
#include <sybdb.h>
#include <string.h>

int format_utf8(char *src)
{
	int left = 0;
	int ckpos = 0;
	int i = 0;
	int src_len = strlen(src);
	unsigned char k = 0;
	for(i=0; i<src_len ; i++){
		k = (unsigned char)(src[i]);
		if(left == 0){
			if(k<128){                      //0xxxxxxx
				left = 0;
			}else if(k>=192 && k<224){      //110xxxxx 10xxxxxx 
				left = 1;
			}else if(k>=224 && k<240){      //1110xxxx 10xxxxxx 10xxxxxx 
				left = 2;
			}else{
				break;
			}
		}else if(left>0 && k>=128 && k<192){
			left --;
		}else{
			break;
		}
		if(left == 0){
			ckpos = i+1;
		}
	}
	src[ckpos] = 0;
	if(ckpos < i){
		fprintf(stderr, "malformed char %d/%d \n",ckpos,i);
		return -1;
	}
	return 0;
}

int main(int argc, char** argv)
{
	DBPROCESS	*dbp = NULL;
	LOGINREC	*lgp = NULL;
	DBINT		userid = 0;
	DBCHAR		firstname[128] = {0}; 
	DBCHAR		lastname[128] = {0}; 
	DBCHAR		disname[128] = {0}; 
	DBINT		hasimg = 0;
	DBCHAR		username[128] = {0}; 
	DBCHAR		email[128] = {0}; 
	RETCODE		retcode = 0;

	fflush(stdout); 
	/* Initialize DB-Library. */ 
	if (dbinit() == FAIL){
		printf("db init failed\n");
		return(1);
	} 
	lgp = dblogin(); 
	if(lgp == NULL){
		printf("db login failed\n");
		return(2);
	}
	DBSETLUSER(lgp, "lampadonet");
	DBSETLPWD(lgp,"sdPx#&4iov%");
	dbp = dbopen(lgp, "myspaceusersearch");
	if(dbp == NULL){
		printf("db open failed\n");
		return(3);
	}
	if(dbuse(dbp, "myspaceusersearch") == FAIL){
		printf("db use failed\n");
		return(4);
	}
	printf("db open success\n");
//	dbcmd(dbp, "SELECT InviteUserid, InvitedUserid, SignupTime FROM InvitedLog WHERE SignUpTime > '2008-11-15 00:00:00'"); 
	dbcmd(dbp, "SELECT [UserID],replace(replace(replace([firstname],char(13),''),char(9),''),char(10),''),replace(replace(replace([lastname],char(13),''),char(9),''),char(10),''),replace(replace(replace([displayname],char(13),''),char(9),''),char(10),''),[hasimage],[username],replace(replace(replace([country],char(13),''),char(9),''),char(10),''),[Email] FROM [myspaceusersearch].[dbo].[UserSearch_Int]"); 
	dbsqlexec(dbp);
	FILE * fp = fopen("/data1/var/sql/usersearch/search.txt","w+");
	if(fp == NULL){
		printf("open file failed\n");
		return 5;
	}
	int i = 0;
	while ((retcode = dbresults(dbp)) != NO_MORE_RESULTS){
		if (retcode == SUCCEED){
			dbbind(dbp, 1,INTBIND, (DBINT)4, (BYTE *)&userid);
			dbbind(dbp, 2,NTBSTRINGBIND, (DBINT)64, (BYTE *)&firstname);
			dbbind(dbp, 3,NTBSTRINGBIND, (DBINT)64, (BYTE *)&lastname);
			dbbind(dbp, 4,NTBSTRINGBIND, (DBINT)64, (BYTE *)&disname);
			dbbind(dbp, 5,INTBIND, (DBINT)4, (BYTE *)&hasimg);
			dbbind(dbp, 6,NTBSTRINGBIND, (DBINT)64, (BYTE *)&username);
			dbbind(dbp, 8,NTBSTRINGBIND, (DBINT)64, (BYTE *)&email);
			printf("\n");
			while (dbnextrow(dbp) != NO_MORE_ROWS){
				format_utf8(disname);
				format_utf8(lastname);
				format_utf8(firstname);
				fprintf(fp, "%d\t%s\t%s\t%s\t%d\t%s\t \t%s\n", 
					userid, firstname, lastname, disname, hasimg, username, email);
				if((++i)%500000 == 0){
					printf("fetch %d rows\n", i);
				}
			}
		}
	}
	printf("--fetch %d rows\n", i);
	dbexit();
	return(0);
}
